package org.dromara.common.excel.convert

import cn.hutool.core.convert.Convert
import cn.idev.excel.converters.Converter
import cn.idev.excel.enums.CellDataTypeEnum
import cn.idev.excel.metadata.GlobalConfiguration
import cn.idev.excel.metadata.data.ReadCellData
import cn.idev.excel.metadata.data.WriteCellData
import cn.idev.excel.metadata.property.ExcelContentProperty
import io.github.oshai.kotlinlogging.KotlinLogging
import java.math.BigDecimal

/**
 * @updater LikeYouDo
 * @date 2025/1/14 15:31
 */
open class ExcelBigNumberConvert : Converter<Long> {
    companion object {
        private val log = KotlinLogging.logger { }
    }

    override fun supportJavaTypeKey(): Class<Long> {
        return Long::class.java
    }

    override fun supportExcelTypeKey(): CellDataTypeEnum {
        return CellDataTypeEnum.STRING
    }

    override fun convertToJavaData(
        cellData: ReadCellData<*>,
        contentProperty: ExcelContentProperty?,
        globalConfiguration: GlobalConfiguration?
    ): Long {
        return Convert.toLong(cellData.data)
    }

    override fun convertToExcelData(
        obj: Long?,
        contentProperty: ExcelContentProperty?,
        globalConfiguration: GlobalConfiguration?
    ): WriteCellData<Any> {
        if (obj != null) {
            val str = Convert.toStr(obj)
            if (str.length > 15) {
                return WriteCellData(str)
            }
        }
        val cellData = WriteCellData<Any>(BigDecimal(obj!!))
        cellData.type = CellDataTypeEnum.NUMBER
        return cellData
    }
}
